package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class users {


    private int Uid;

    private String Uaccount;

    private String Uname;

    private String Upassword;

    private int Uage;

    public int getUage() {
        return Uage;
    }

    public void setUage(int uage) {
        Uage = uage;
    }

    public String getUsex() {
        return Usex;
    }

    public void setUsex(String usex) {
        Usex = usex;
    }

    private String Usex;

    public String getUname() {
        return Uname;
    }

    public void setUname(String uname) {
        Uname = uname;
    }

    public int getCno() {
        return Cno;
    }

    public void setCno(int cno) {
        Cno = cno;
    }

    private String Ucate;

    private int Cno;

    public users(String uaccount, String upassword, String ucate){
        Uaccount = uaccount;
        Upassword = upassword;
        Ucate = ucate;
    }

    public users(String uaccount,String uname){
        Uname = uname;
        Uaccount = uaccount;
    }

    public users(String uaccount,String uname,String upassword,int uage,String usex,String ucate){
        Uaccount = uaccount;
        Upassword = upassword;
        Ucate = ucate;
        Uname = uname;
        Uage=uage;
        Usex=usex;
    }

    public int getUid() {
        return Uid;
    }

    public void setUid(int uid) {
        Uid = uid;
    }

    public String getUaccount() {
        return Uaccount;
    }

    public void setUaccount(String uaccount) {
        Uaccount = uaccount;
    }

    public String getUpassword() {
        return Upassword;
    }

    public void setUpassword(String upassword) {
        Upassword = upassword;
    }

    public String getUcate() {
        return Ucate;
    }

    public void setUcate(String ucate) {
        Ucate = ucate;
    }

    public boolean findPswByUaccount(users user){

        Connection con=jdbcUtil.jdbcUtil.getConnection();

        PreparedStatement pstm=null;

        ResultSet rs=null;

        String searchPsw="";

        String cate="";

        try {
            pstm=con.prepareStatement("SELECT * FROM users WHERE Uaccount=?");

            pstm.setString(1,user.Uaccount);

            rs=pstm.executeQuery();

            while (rs.next()){
                searchPsw=rs.getString("Upassword");
                cate=rs.getString("Ucate");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("Login失败--->users.java-->findPswByUaccount()");
            return false;
        }finally {
            jdbcUtil.jdbcUtil.close(null,pstm,rs);
        }

        if(searchPsw.equals(user.Upassword) && cate.equals(user.getUcate())){
            System.out.println("Login成功-->users.java-->findPswByUaccount()");
            return true;
        }else{
            System.out.println("Login失败--->users.java-->findPswByUaccount()");
            return false;
        }
    }

    public static boolean addUserInfo(String uaccount,String uname,String upassword,int uage,String usex,String ucate){
        Connection con=jdbcUtil.jdbcUtil.getConnection();

        PreparedStatement pstm=null;

        ResultSet rs=null;

        int num=0;

        try {
            pstm=con.prepareStatement("INSERT INTO users (Uaccount,Uname,Upassword,Uage,Usex,Ucate) VALUES (?,?,?,?,?,?)");

            pstm.setString(1,uaccount);
            pstm.setString(2,uname);
            pstm.setString(3,upassword);
            pstm.setInt(4,uage);
            pstm.setString(5,usex);
            pstm.setString(6,ucate);

            num=pstm.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("失败--->classes.java-->addClassInfo()");
            return false;
        }finally {
            jdbcUtil.jdbcUtil.close(null,pstm,rs);
        }

        if(num!=0){
            return true;
        }else{
            return false;
        }
    }
}
